package com.xianggu.controller;

import com.plugin.MyPlugin;
import com.xianggu.common.XiangGuService;
import com.xianggu.demo.dto.UserDto;
import com.xianggu.mybatis.paging.Page;
import com.xianggu.spring.annotation.*;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Map;

/**
 * @author chengjy
 * @date 2025/3/9
 */
@RequestMapping("/user")
@Controller
public class UserController {

    MyPlugin myPlugin;

    @AutoWired
    private XiangGuService xiangGuService;

    @GetMapping("/get")
    public UserDto getUserInfo(String id) {
        if(myPlugin != null){
            myPlugin.execute();
        }
        return xiangGuService.getUserInfo(id);
    }
    @GetMapping("/getUserList")
    public Page<UserDto> getUserList(@RequestParam("id") String id , @RequestParam("pageNum") int pageNum , @RequestParam("pageSize") int pageSize) {
        if(myPlugin != null){
            myPlugin.execute();
        }
        return xiangGuService.getUserList(id , pageNum , pageSize);
    }
    @GetMapping("/getUserListMap")
    public List<Map<String,Object>> getUserListMap(@RequestParam("id") String id , @RequestParam("pageNum") int pageNum , @RequestParam("pageSize") int pageSize) {
        if(myPlugin != null){
            myPlugin.execute();
        }
        return xiangGuService.getUserListMap(id , pageNum , pageSize);
    }
    public String save(@RequestBody UserDto userDto) {
        if(myPlugin != null){
            myPlugin.execute();
        }
        System.out.println("执行新增数据："+ userDto);
        return xiangGuService.saveUser(userDto);
    }

    @GetMapping("/loadPlugin")
    public void loadPlugin(@RequestParam("path") String path ) {
        File file = new File(path);
        try {
            URLClassLoader classLoader = new URLClassLoader(new URL[]{file.toURI().toURL()});
            InputStream resourceAsStream = classLoader.getResourceAsStream("xianggu.plugin");
            String className = new String(resourceAsStream.readAllBytes());

            Class<?> aClass = classLoader.loadClass(className);
            if(MyPlugin.class.isAssignableFrom(aClass)){
                Object object = aClass.getConstructor().newInstance();
                myPlugin = (MyPlugin) object;
            }
            System.out.println("加载插件成功！"+ aClass.getName());
        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.out.println("文件路径加载失败！");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}
